import java.awt.Color;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import com.vividsolutions.jts.geom.Coordinate;


public class Util {
	
	public static Coordinate[] path2coord(GpsTrack track) {
		Coordinate[] coord = new Coordinate[track.nPoints()]; 
		for (int i = 0; i < coord.length; i++) {
			coord[i] = new Coordinate(track.getPoint(i).getLongitude(), 
					track.getPoint(i).getLatitude(), track.getPoint(i).getElevation());
		}
		return coord;
	}

	public static GpsTrack coord2path(Coordinate[] coord) {
		Vector path = new Vector(coord.length);
		for (int i = 0; i < coord.length; i++) {
			path.add(new Location(coord[i].y, coord[i].x, coord[i].z));			
		}
		return new GpsTrack(path);
	}
	
	public static CoordinateTime[] path2coordTime(GpsTrack track) {
		CoordinateTime[] ct = new CoordinateTime[track.nPoints()]; 
		for (int i = 0; i < ct.length; i++) {
			ct[i] = new CoordinateTime(track.getPoint(i).getLongitude(), 
					track.getPoint(i).getLatitude(), track.getPoint(i).getElevation(),
					track.getPoint(i).getTimeString());
		}
		return ct;
	}

	public static GpsTrack coordTime2path(CoordinateTime[] coord) {
			Vector path = new Vector(coord.length);
			for (int i = 0; i < coord.length; i++) {
				path.add(new Location(coord[i].y, coord[i].x, coord[i].z, coord[i].getDateTimeString()));			
			}
			return new GpsTrack(path);
		}

	public static GpsTrack coordTime2path(Coordinate[] coord,
			CoordinateTime[] coord_basis) {
		Vector path = new Vector(coord.length);
		int j = -1;
		for (int i = 0; i < coord.length; i++) {
			do {
				j++;
			} while (!coord[i].equals3D(coord_basis[j]));
			path.add(new Location(coord[i].y, coord[i].x, coord[i].z, coord_basis[j].getDateTimeString()));			
		}
		return new GpsTrack(path);
	}
	
	public static double angle2meter() {
		return 1000.0 * 40000.0 / 360.0;
	}

	public static double meter2angle() {
		return 1.0 / (1000.0 * 40000.0 / 360.0);
	}

	static String format(double x, int zahlStellen) {
		final NumberFormat defForm = NumberFormat.getInstance();
		if (Double.isNaN(x)) {
			return "NaN";
		}

		defForm.setMaximumFractionDigits(zahlStellen);
		defForm.setMinimumFractionDigits(zahlStellen);
		return defForm.format(x);
	}

    static String formatDate(Date d) {
    	SimpleDateFormat sd = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
    	return sd.format(d);
    }

    static String formatDateYMD(Date d) {
    	SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	return sd.format(d);
    }
    
    static Color getColor(double z, double zmin, double zmax, Color colormin, Color colormax) {
        return new Color(interpol(z, zmin, zmax, colormin.getRed(), colormax.getRed()),
        		interpol(z, zmin, zmax, colormin.getGreen(), colormax.getGreen()),
        		interpol(z, zmin, zmax, colormin.getBlue(), colormax.getBlue()));
    }
    
    static int interpol(double z, double zmin, double zmax, int from, int to) {
 //   	System.out.println(z + " " + zmin + " " + zmax + " / " + from + " " + to);
    	int k = (int) (from + Math.round(((z-zmin) / (zmax - zmin)) * ((double) to - (double) from)));
    	return constrain(k, 0, 255);
    }
    
    static int constrain(int k, int lower, int upper) {
    	if (k < lower) return lower;
    	if (k > upper) return upper;
    	return k;    	
    }

}
